package cn.ztuo.bitrade.controller.lottery;

import cn.ztuo.bitrade.annotation.AccessLog;
import cn.ztuo.bitrade.constant.AdminModule;
import cn.ztuo.bitrade.constant.SysConstant;
import cn.ztuo.bitrade.controller.BaseController;
import cn.ztuo.bitrade.entity.Admin;
import cn.ztuo.bitrade.entity.Lottery;
import cn.ztuo.bitrade.service.LotteryService;
import cn.ztuo.bitrade.service.WinningRecordService;
import cn.ztuo.bitrade.util.MessageResult;
import cn.ztuo.bitrade.vo.WinningRecordVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

/**
 * @author: Xiazt
 * @date: 2020-04-23 10:18
 */
@RestController
@RequestMapping("lottery")
public class LotteryController extends BaseController {

    @Autowired
    private LotteryService lotteryService;

    @Autowired
    private WinningRecordService winningRecordService;

    @RequiresPermissions("lottery:page-query")
    @RequestMapping(value = "page-query")
    @AccessLog(module = AdminModule.AWARDSSETTING, operation = "分页获取开奖记录")
    public MessageResult getByPage(@SessionAttribute(SysConstant.SESSION_ADMIN) Admin admin, Lottery lottery) {
        return success(lotteryService.getByPage(lottery));
    }

    @RequiresPermissions("lottery:saveOrUpdate")
    @RequestMapping(value = "saveOrUpdate")
    @AccessLog(module = AdminModule.AWARDSSETTING, operation = "添加或修改开奖记录")
    public MessageResult saveOrUpdate(@SessionAttribute(SysConstant.SESSION_ADMIN) Admin admin, Lottery lottery) {
        if (lotteryService.findByNumber(lottery.getNumber()) != null) return error("开奖号码重复！");
        if (lotteryService.findByPeriods(lottery.getPeriods()) != null) return error("开奖期号重复！");
        return success(lotteryService.saveOrUpdate(lottery));
    }

    @RequiresPermissions("lottery:delete")
    @RequestMapping(value = "delete")
    @AccessLog(module = AdminModule.AWARDSSETTING, operation = "删除开奖记录")
    public MessageResult delete(@SessionAttribute(SysConstant.SESSION_ADMIN) Admin admin, Long id) {
        lotteryService.delete(id);
        return success();
    }

    // 中奖列表
    @RequiresPermissions("lottery:winningRecordList")
    @RequestMapping(value = "winningRecordList")
    @AccessLog(module = AdminModule.LOTTERY, operation = "获取中奖纪录")
    public MessageResult winningRecordList(@SessionAttribute(SysConstant.SESSION_ADMIN) Admin admin, WinningRecordVO winningRecordVO) {
        return success(winningRecordService.getByPage(winningRecordVO));
    }

}
